//
//  MobileRegistFirstStepController.m
//  QRMaster
//
//  Created by myApple on 16/3/2.
//  Copyright © 2016年 myApple. All rights reserved.
//

#import "MobileRegistFirstStepController.h"
#import "MobileRegistFirstStepView.h"
#import "AreaChooseController.h"
#import "RegistFirstStepViewController.h"
#import "RegistLastViewController.h"
#import "AgreeViewController.h"
#import "RequestNetWork.h"
#import "MyRsa.h"

@interface MobileRegistFirstStepController ()<MobileRegistFirstStepDelegate>
{
    MobileRegistFirstStepView * MobileView;
    NSMutableDictionary * dict;
}

@end

@implementation MobileRegistFirstStepController

- (void)viewDidLoad {
    [super viewDidLoad];
    dict = [[NSMutableDictionary alloc]init];
    self.view.backgroundColor = RGBA(237, 238, 241, 1);
     self.title = NSLocalizedString(@"registByMobile", nil);
    [self prepareView];
    // Do any additional setup after loading the view.
}

-(void)prepareView
{
    MobileView = [[MobileRegistFirstStepView alloc]initWithFrame:CGRectMake(0, 0, UI_SCREEN_WIDTH, UI_SCREEN_HEIGHT - 64)];
    MobileView.delegate = self;
    [self.view addSubview:MobileView];
}


-(void)Resgsit:(NSMutableDictionary *)postDict
{
    if ([self IsHaveNetwork]) {
        return;
    }
    [dict setValue:[postDict objectForKey:@"mobile"] forKey:@"mobile"];
    [dict setValue:[postDict objectForKey:@"areaCode"] forKey:@"areaCode"];
    [self startWaiting:nil color:nil addToView:self.view];
    RequestNetWork * rnk = [[RequestNetWork alloc]init];
    [rnk initWithPath:POST(REGISTERVCODE) params:dict block:^(id downLoadData, id error) {
        [self stopWaiting];
        if ([downLoadData objectForKey:@"data"] != nil &&[@"" isEqualToString:[downLoadData objectForKey:@"data"]]==NO) {
            RegistLastViewController * rlvc = [[RegistLastViewController alloc]init];
            rlvc.passWord = [self getPassword:[postDict objectForKey:@"password"]];
            rlvc.mobileNumber = [postDict objectForKey:@"mobile"];
            rlvc.areaCode = [postDict objectForKey:@"areaCode"];
            [self.navigationController pushViewController:rlvc animated:YES];
        }
        else if (error != nil) {
            [PromptInfoView showMessage:[error objectForKey:@"info"] superView:self.view];
        }
    }];
}

-(NSString *)getPassword:(NSString *)password
{
    MyRsa * rsa = [[MyRsa alloc]init];
    if (rsa != nil) {
        NSString * encryptPWD = [rsa RSAEncrypotoTheData:[NSString stringWithFormat:@"12345678%@",password] key:publicKeyString];
        encryptPWD = [encryptPWD stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
        return encryptPWD;
    }
    else
        return nil;
    
}


-(void)resgsitByEmail
{
    RegistFirstStepViewController * rfsv = [[RegistFirstStepViewController alloc]init];
    [self.navigationController pushViewController:rfsv animated:YES];
}

-(void)choiceArea
{
    AreaChooseController * acc = [[AreaChooseController alloc]init];
    [self.navigationController pushViewController:acc animated:YES];
    
    acc.areaCode = ^(NSString * areaCodeId,NSString * areaOrCountry)
    {
        [MobileView.areaTextBtn setTitle:areaOrCountry forState:0];
        MobileView.areaCodeTextField.text = areaCodeId;
    };
}

-(void)pushAgreement
{
    AgreeViewController * avc = [[AgreeViewController alloc]init];
    [self.navigationController pushViewController:avc animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
